<?php
if (!defined('FosfoaSystem')){exit(1);}
import($GLOBALS['spConfig']['controller_path'] . '/general.php');
class orders extends general
{
    public function __construct()
    {
        parent::__construct();
        $this->tpl_title = T("Dashboard");
    }
    public function index()
    {
        $this->tt = "订单管理";
        $type=$this->spArgs("type",0);
        $current_page = $this->spArgs("page", 1);
        $page_size = $this->spArgs("size", 20);
        $obj = spClass("orderModel");
        if($type==0){
         $conditions = array('1' => 1);   
        }else{
        $conditions = array('orders_status' => $type);
        }
        $arr = $obj->spPager($current_page, $page_size)->findAll($conditions, 'orders_id desc');
        foreach($arr as $k=>$v)
        {
           $status=spClass('orders_statusModel')->Detail($v['orders_status']);
           $arr[$k]['orders_status']=$status['orders_status_name'];
        }
        
        $arrBar = $obj->spPager()->getPager();
        $this->tPageBar = $arrBar;
        $this->arr = $arr;
        $this->orders_id=$orders_id;
        $this->display("admin/orders_index.html");
    }
    function view()
    {
        $orders_id=$this->spArgs('orders_id',0);
        if($orders_id==0){
            $this->error('订单不存在!!');
        }
        $this->statusall=spClass('orders_statusModel')->findAll();
        $order=spClass('orderModel')->find('orders_id='.$orders_id);
        $conditions = array( 'orders_id' => $order['orders_id'] );
        $orderslist=spClass('orders_productsModel')->findAll($conditions,'orders_id desc');
        $conditions2 = array( 'orders_sn' => $order['orders_sn'] );
        $orders_status=spClass('orders_status_historyModel')->findAll($conditions2);
        foreach ($orders_status as $k => $v)
                {
                    $status=spClass('orders_statusModel')->Detail($v['orders_status_id']);
                    $orders_status[$k]['orders_status_id']=$status['orders_status_name'];
                }
        $this->ordersstatus=$orders_status;
        $this->order=$order;
        $this->orderslist=$orderslist;
        $this->display("admin/orders_view.html");
    }
    function save()
    {
        $orders_id = $this->spArgs("orders_id",0);
        $orders_sn = $this->spArgs("orders_sn",0);
        $status = $this->spArgs("status");
        $notify = $this->spArgs('notify');
        $notify_comments = $this->spArgs('notify_comments');
        $comments = $this->spArgs("comments");
        $customers_email_address = $this->spArgs('customers_email_address');
        $customers_name = $this->spArgs('customers_name');
        if($orders_id==0){
            $this->error('订单不存在');
        }
        $data = array("orders_status" => $status);
        $edittime=time();
        $data2 = array('orders_sn' => $orders_sn,"orders_status_id" => $this->spArgs("status"),'date_added'=>$edittime);             
            $Obj = spClass('orderModel');
            $Obj2=spClass('orders_status_historyModel');   
            $condition = array('orders_id' => $orders_id);
            $Obj->update($condition, $data);
            if($notify_comments=='on')
            {
                $data2['comments']=$comments;
            }
            $Obj2->create($data2);
            //echo $Obj2->dumpSql();
           if($notify==1){
            //这里发信给顾客
                    $mail = spClass('email_templateModel');
                    $email_address=$customers_email_address;
                    
                    $fullname=$customers_name;
                    $edittime=date("l dS \of F Y h:i:s A",$edittime);
                    $statusarr=spClass('orders_statusModel')->Detail($status);
                    $status=$statusarr['orders_status_name'];
                    $body='';                                        
                    //$body = $mail->mail_replace($body, $email_address, $fullname);
                    //echo $body;
                    //$body = str_replace("#ORDER#", $order_sn, $body);
                    $subject='Order Update #'.$orders_sn;
                    $body = STORENAME.'<br />------------------------------<wbr>------------------------<br />Order Number: '.$orders_sn.'<br /><br />Detailed Invoice:<br /><a target="_blank" href="'.WEBURL.WEBDIR.'main/AccountHistoryInfo.html?order_id='.$orders_id.'">'.WEBURL.WEBDIR.'main/AccountHistoryInfo.html?order_id='.$orders_id.'</a><br /><br />Date Ordered: '.$edittime.'<br />';
                            
                    if($comments<>''){
                    $body=$body.'<br />The comments for your order are: '.$comments.'<br />';
                    }
                    $body=$body.'<br />Your order has been updated to the following status:<br />New status: '.$status;                 
                    $body=$body.'<br /><br />Please reply to this email if you have any questions.<br /><br />'.T('EMAIL_DISCLAIMER_NEW_CUSTOMER').'<br/>'.T('CAN-SPAM');
                    //echo $body;
                    //exit;
                    if ($mail->Sendmail($email_address, $fullname, REPLYEMAIL,STORENAME, $subject, $body))
                    {
                        $sendmailok = true;
                    }
           } 
           $msg="修改订单状态:订单号：".$orders_sn."状态:".$status."内容:".$comments;
           Savelog($msg); 
           $this->success('修改成功');  
    }     
    function del()
    {
        $Obj = spClass("orderModel");
        $order_products=spClass('orders_productsModel');
        $orders_status=spClass("orders_status_historyModel");
        $id2 = $this->spArgs("orders_id", 0);
        if ($id2 == 0)
        {
            $ids = $this->spArgs("selids");
            $idsarray = explode(',', $ids);
        
            foreach ($idsarray as $id)
            {
                $conditions = array('orders_id' => $id);
                $Obj->delete($conditions); // 删除记录
                $order_products->delete($conditions);//删除订单产品记录
                $orders_status->delete($conditions);//删除订单状态历史
            }
        } else
        {

            $conditions = array('orders_id' => $id2);
                $Obj->delete($conditions); // 删除记录
                $order_products->delete($conditions);//删除订单产品记录
                $orders_status->delete($conditions);//删除订单状态历史
        }
        $msg="删除定单".$id2;
        Savelog($msg);
        $this->success(T('Successfully  deleted!'), spUrl("orders", "index"));

    }  
}